package com.gyul.findmystock.db;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.gyul.findmystock.entity.TradeEntity;


public class DaoTrade extends DataAccessObject<TradeEntity> {
	public static final String TRADE_TABLE = "TradeTable";
	public static final String TRADE_SYMBOL = "TradeSymbol";
	public static final String TRADE_NAME = "TradeName";
	public static final String TRADE_PRICE = "TradePrice";
	public static final String TRADE_COUNT = "TradeCount";
	public static final String TRADE_GOAL_PRICE = "TradeGoalPrice";
	
	public static final String CREATE_QUERY = "CREATE TABLE IF NOT EXISTS " + TRADE_TABLE + " ( " +
			TRADE_SYMBOL + " TEXT PRIMARY KEY, " +
			TRADE_NAME + " TEXT NOT NULL, " + 
			TRADE_PRICE + " INT NOT NULL, " +
			TRADE_COUNT + " INT NOT NULL, " +
			TRADE_GOAL_PRICE + " INT NOT NULL);";
	
	public static final String DROP_QUERY = "DROP TABLE IF EXISTS " + TRADE_TABLE;
	
	public DaoTrade(String TAG) {
		super(TAG + " - DaoTrade");
	}

	public void createTable() {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(CREATE_QUERY);
		//db.close();
	}
	
	public void dropTable() {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(DROP_QUERY);
		//db.close();
	}
	
	public void insert(TradeEntity data) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("INSERT INTO " + TRADE_TABLE + 
				" (" + TRADE_SYMBOL + ", " + TRADE_NAME + ", " +
						TRADE_PRICE + ", " + TRADE_COUNT + ", " + TRADE_GOAL_PRICE + ") " +
				"VALUES ('" + data.symbol + "', '" + data.name + "', " +
						data.price + ", " + data.count + ", " + data.goalPrice + ");");
		//db.close();
	}
	
	public void update(TradeEntity data) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("INSERT OR REPLACE INTO " + TRADE_TABLE + 
				" (" + TRADE_SYMBOL + ", " + TRADE_NAME + ", " +
						TRADE_PRICE + ", " + TRADE_COUNT + ", " + TRADE_GOAL_PRICE + ") " +
				"VALUES ('" + data.symbol + "', '" + data.name + "', " +
						data.price + ", " + data.count + ", " + data.goalPrice + ");");
		//db.close();
	}
	
	public ArrayList<TradeEntity> select() {
		SQLiteDatabase db = helper.getReadableDatabase();
		ArrayList<TradeEntity> list = new ArrayList<TradeEntity> ();
		Cursor c = db.rawQuery("SELECT * FROM " + TRADE_TABLE + ";", null);
		while ( c.moveToNext() ) {
			list.add(new TradeEntity(c.getString(c.getColumnIndex(TRADE_SYMBOL)),
								c.getString(c.getColumnIndex(TRADE_NAME)),
								c.getInt(c.getColumnIndex(TRADE_PRICE)),
								c.getInt(c.getColumnIndex(TRADE_COUNT)),
								c.getInt(c.getColumnIndex(TRADE_GOAL_PRICE))));
		}
		
		c.close();
		//db.close();
		
		return list; 
	}
	
	public TradeEntity get(String symbol) {
		SQLiteDatabase db = helper.getReadableDatabase();
		TradeEntity entity = new TradeEntity ();
		Cursor c = db.rawQuery("SELECT * FROM " + TRADE_TABLE + " WHERE " + TRADE_SYMBOL + " = '" + symbol + "';", null);
		if ( c.moveToNext() ) {
			entity = new TradeEntity(c.getString(c.getColumnIndex(TRADE_SYMBOL)),
								c.getString(c.getColumnIndex(TRADE_NAME)),
								c.getInt(c.getColumnIndex(TRADE_PRICE)),
								c.getInt(c.getColumnIndex(TRADE_COUNT)),
								c.getInt(c.getColumnIndex(TRADE_GOAL_PRICE)));
		}
		
		c.close();
		//db.close();
		
		return entity; 
	}
	
	public void delete(String symbol, int price) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("DELETE FROM " + TRADE_TABLE + 
				" WHERE " + TRADE_SYMBOL + " = '" + symbol + "' AND " +
							TRADE_PRICE + " = " + price + ";");
		//db.close();
	}
}
